package qnb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import qnb._common.easyui.EasyuiGridData;
import qnb._common.easyui.EasyuiPageVO;
import qnb._common.vo.MessageVOFactory;
import qnb.entity.Classroom;
import qnb.service.ClassroomService;

import java.util.List;


@Controller
@RequestMapping("/classroom")
public class ClassroomController {

   @Autowired
    private ClassroomService classroomService;


    @RequestMapping("/forward/classroomAdd")
    public String forwardTAdd() {
        return "classroom/classroomAdd";
    }

    @RequestMapping("/forward/classroomList")
    public String forwardList() {
        return "classroom/classroomList";
    }

//    //===================================== 上面的为跳转方法====================================

    /**
     *  分页查询全部
     * @param vo 分页条件
     * @return
     */
    @PostMapping("/selectAllClassroom")
    @ResponseBody
    public Object selectAllClassroom(EasyuiPageVO vo) {
        if (vo.isSqlError()) {
            System.out.println("======================");
            return null;
        }
        String whereSql = vo.getWhereSql();
        return EasyuiGridData.getEasyuiGridData(classroomService.selectCountByClassroom(whereSql),
                classroomService.selectAllClassroom(whereSql,vo.getOrderbySQL(),vo.getStart(),vo.getRows()));
    }


    /**
     *  绑定教室信息到下拉框中
     * @return
     */
    @PostMapping("/selectAllClassroomByName")
    @ResponseBody
    public Object selectAllClassroomByName(){
        List<Classroom> list = classroomService.selectAllClassroomByName();
        return list;
    }

    /**
     *  根据id查询指定信息并回显到页面
     * @param classroomId
     * @return
     */
    @RequestMapping("/selectClassroomById")
    @ResponseBody  //异步请求
    public Classroom selectGradeById(Integer classroomId) {
        if (null == classroomId || classroomId < 0) {
            return null;
        }
        return classroomService.selectClassroomById(classroomId);
    }

    /**
     *  添加功能
     * @param classroom
     * @return
     */
    @RequestMapping("/insertClassroom")
    @ResponseBody
    public String insertClassroom(Classroom classroom) {
       return MessageVOFactory.getAddMessage(classroomService.insertClassroom(classroom));
    }

    /**
     * 修改班级信息
     * @param classroom
     * @return
     */
    @PostMapping("/updateClassroom")
    @ResponseBody
    public String updateClassroom(Classroom classroom){
       return MessageVOFactory.getUpdateMessage(classroomService.updateClassroom(classroom));
    }

    /**
     *  删除功能
     * @param
     * @return
     */
    @RequestMapping("/deleteClassroom")
    @ResponseBody
    public String deleteClassroom(Integer classroomId){
        if (null == classroomId || classroomId < 0) {
            return null;
        }
        return MessageVOFactory.getDellMessage(classroomService.deleteClassroom(classroomId));
    }


}
